﻿using GKQC.DbHelper.DbModels;
using GKQC.DbHelper.ErpDbModels;
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;

namespace GKQC.DbHelper.ViewModels
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T">datamodel</typeparam>
    public class ViewModelTrans<T> where T : new()
    {
        static string trueString = "1";
        static string falseString = "2";

        public static List<T> ElectricViewToData(long headId, int sNo, List<ElectricViewModel> electricViewModels)
        {

            List<T> datas = new List<T>();
            Type type = typeof(T);
            PropertyInfo headIdProperty = type.GetProperty("HeadId");
            PropertyInfo sNoProperty = type.GetProperty("SNo");
            PropertyInfo itemCodeProperty = type.GetProperty("ItemCode");
            PropertyInfo valueProperty = type.GetProperty("Value");
            PropertyInfo validProperty = type.GetProperty("Valid");

            if (electricViewModels != null && electricViewModels.Count > 0)
            {
                bool hasCap = electricViewModels.Where(t => t.CAP != 0).Any();
                bool hasDf = electricViewModels.Where(t => t.DF != 0).Any();
                bool hasLc = electricViewModels.Where(t => t.LC != 0).Any();
                bool hasLc2 = electricViewModels.Where(t => t.LC2 != 0).Any();
                bool hasEsr = electricViewModels.Where(t => t.ESR != 0).Any();
                bool hasEsr2 = electricViewModels.Where(t => t.ESR2 != 0).Any();
                bool hasZ = electricViewModels.Where(t => t.Z != 0).Any();
                bool hasZ2 = electricViewModels.Where(t => t.Z2 != 0).Any();
                bool hasH = electricViewModels.Where(t => t.H != 0).Any();
                bool hasL1 = electricViewModels.Where(t => !string.IsNullOrWhiteSpace(t.L1)).Any();
                bool hasD = electricViewModels.Where(t => t.D != 0).Any();
                bool hasL = electricViewModels.Where(t => t.L != 0).Any();
                bool hasC = electricViewModels.Where(t => t.C != 0).Any();

                electricViewModels.ForEach(e =>
                {
                    T t;
                    if (hasCap)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "CAP", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.CAP, null);
                        if (validProperty != null) validProperty.SetValue(t, e.CAPisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (hasDf)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "DF", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.DF, null);
                        if (validProperty != null) validProperty.SetValue(t, e.DFisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (hasLc)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "LC", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.LC, null);
                        if (validProperty != null) validProperty.SetValue(t, e.LCisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }
                    if (hasLc2)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "LC2", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.LC2, null);
                        if (validProperty != null) validProperty.SetValue(t, e.LC2isValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (hasZ)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "Z", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.Z, null);
                        if (validProperty != null) validProperty.SetValue(t, e.ZisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (hasZ2)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "Z2", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.Z2, null);
                        if (validProperty != null) validProperty.SetValue(t, e.Z2isValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (hasEsr)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "ESR", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.ESR, null);
                        if (validProperty != null) validProperty.SetValue(t, e.ESRisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }
                    if (hasEsr2)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "ESR2", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.ESR2, null);
                        if (validProperty != null) validProperty.SetValue(t, e.ESR2isValid ? trueString : falseString, null);
                        datas.Add(t);
                    }
                    if (hasH)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "H", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.H, null);
                        if (validProperty != null) validProperty.SetValue(t, trueString, null);
                        datas.Add(t);
                    }
                    if (hasL1)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "L1", null);
                        double value = 0d;
                        if (double.TryParse(e.L1, out value))
                        {
                            if (valueProperty != null)
                                valueProperty.SetValue(t, value, null);
                            if (validProperty != null) validProperty.SetValue(t, trueString, null);
                        }
                        else
                        {
                            if (validProperty != null) validProperty.SetValue(t
                                , (string.IsNullOrWhiteSpace(e.L1) ? string.Empty: e.L1)
                                , null);
                        }
                        datas.Add(t);
                    }

                    if (hasD)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "D", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.D, null);
                        if (validProperty != null) validProperty.SetValue(t, e.DisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (hasL)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "L", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.L, null);
                        if (validProperty != null) validProperty.SetValue(t, e.LisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (hasC)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "C", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.C, null);
                        if (validProperty != null) validProperty.SetValue(t, e.CisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }
                });
            }
            
            return datas;
        }

        public static List<T> SizeViewToData(long headId, int sNo, List<SizeViewModel> sizeViewModels)
        {
            List<T> datas = new List<T>();
            Type type = typeof(T);
            PropertyInfo headIdProperty = type.GetProperty("HeadId");
            PropertyInfo sNoProperty = type.GetProperty("SNo");
            PropertyInfo itemCodeProperty = type.GetProperty("ItemCode");
            PropertyInfo valueProperty = type.GetProperty("Value");
            PropertyInfo validProperty = type.GetProperty("Valid");
            if (sizeViewModels != null && sizeViewModels.Count > 0)
            {
                sizeViewModels.ForEach(e =>
                {
                    T t;
                    if (e.D > 0)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "D", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.D, null);
                        if (validProperty != null) validProperty.SetValue(t, e.DisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (e.L > 0)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "L", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.L, null);
                        if (validProperty != null) validProperty.SetValue(t, e.LisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (e.P > 0)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "P", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.P, null);
                        if (validProperty != null) validProperty.SetValue(t, e.PisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (e.cp > 0)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "cp", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.cp, null);
                        if (validProperty != null) validProperty.SetValue(t, e.cpisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (e.H > 0)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "H", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.H, null);
                        if (validProperty != null) validProperty.SetValue(t, e.HisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (e.W > 0)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "W", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.W, null);
                        if (validProperty != null) validProperty.SetValue(t, e.WisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (e.C > 0)
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "C", null);
                        if (valueProperty != null) valueProperty.SetValue(t, e.C, null);
                        if (validProperty != null) validProperty.SetValue(t, e.CisValid ? trueString : falseString, null);
                        datas.Add(t);
                    }

                    if (!string.IsNullOrWhiteSpace(e.L1))
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "L1", null);
                        double value = 0d;
                        if (double.TryParse(e.L1, out value))
                        {
                            if (valueProperty != null)
                                valueProperty.SetValue(t, value, null);
                            if (validProperty != null) validProperty.SetValue(t, e.L1isValid ? trueString : falseString, null);
                        }
                        else
                        {
                            if (validProperty != null) validProperty.SetValue(t, e.L1, null);
                        }
                        datas.Add(t);
                    }

                    if (!string.IsNullOrWhiteSpace(e.L2))
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "L2", null);
                        double value = 0d;
                        if (double.TryParse(e.L2, out value))
                        {
                            if (valueProperty != null)
                                valueProperty.SetValue(t, value, null);
                            if (validProperty != null) validProperty.SetValue(t, e.L2isValid ? trueString : falseString, null);
                        }
                        else
                        {
                            if (validProperty != null) validProperty.SetValue(t, e.L2, null);
                        }
                        datas.Add(t);
                    }

                    if (!string.IsNullOrWhiteSpace(e.L3))
                    {
                        t = new T();
                        if (headIdProperty != null) headIdProperty.SetValue(t, headId, null);
                        if (sNoProperty != null) sNoProperty.SetValue(t, sNo, null);
                        if (itemCodeProperty != null) itemCodeProperty.SetValue(t, "L3", null);
                        double value = 0d;
                        if (double.TryParse(e.L3, out value))
                        {
                            if (valueProperty != null)
                                valueProperty.SetValue(t, value, null);
                            if (validProperty != null) validProperty.SetValue(t, e.L3isValid ? trueString : falseString, null);
                        }
                        else
                        {
                            if (validProperty != null) validProperty.SetValue(t, e.L3, null);
                        }
                        datas.Add(t);
                    }
                });
            }
            return datas;
        }

        public static void DataToView(int sNo, List<T> datas
            ,List<ElectricViewModel> electricViewModels,List<SizeViewModel> sizeViewModels)
        {
            if (electricViewModels!= null) electricViewModels.Clear();
            if (sizeViewModels != null) sizeViewModels.Clear();
            if (datas == null || datas.Count == 0) return;

            Type type = typeof(T);
            PropertyInfo headIdProperty = type.GetProperty("HeadId");
            PropertyInfo sNoProperty = type.GetProperty("SNo");
            PropertyInfo itemCodeProperty = type.GetProperty("ItemCode");
            PropertyInfo valueProperty = type.GetProperty("Value");
            PropertyInfo validProperty = type.GetProperty("Valid");
            ElectricViewModel m = null;
            SizeViewModel s;
            double value;
            string valid = string.Empty;

            int capCount = 0;
            int dfCount = 0;
            int lcCount = 0;
            int lc2Count = 0;
            int esrCount = 0;
            int esr2Count = 0;
            int zCount = 0;
            int z2Count = 0;
            int hCount = 0;
            int l1Count = 0;

            foreach (T t in datas)
            {
                if (electricViewModels.Count == 0) electricViewModels.Add(new ElectricViewModel());
                if (sizeViewModels.Count == 0) sizeViewModels.Add(new SizeViewModel());
                string itemCode = itemCodeProperty.GetValue(t, null).ToString().ToLower();
                Type typeL1 = typeof(T);
                switch (itemCode)
                {
                    case "cap":
                        #region
                        capCount++;
                        if (type == typeof(OrtDataModel))
                        {
                            if (electricViewModels.Count >= capCount)
                                m = electricViewModels[capCount - 1];
                            else

                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                        }
                        else
                        {
                            if (electricViewModels.Count(e => e.CAP <= 0) == 0)
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                            else
                            {
                                m = electricViewModels.Where(e => e.CAP <= 0).First();
                            }
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        m.CAP = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        m.CAPisValid = valid == "1" ? true : false;
                        if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        
                        break;
                        #endregion
                    case "df":
                        #region
                        dfCount++;
                        if (type == typeof(OrtDataModel))
                        {
                            if (electricViewModels.Count >= dfCount)
                                m = electricViewModels[dfCount - 1];
                            else
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                        }
                        else
                        {
                            if (electricViewModels.Count(e => e.DF <= 0) == 0)
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                            else
                            {
                                m = electricViewModels.Where(e => e.DF <= 0).First();
                            }
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        m.DF = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        m.DFisValid = valid == "1" ? true : false;
                        if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());

                        break;
                    #endregion
                    case "lc":
                        #region
                        lcCount++;
                        if (type == typeof(OrtDataModel))
                        {
                            if (electricViewModels.Count >= lcCount)
                                m = electricViewModels[lcCount - 1];
                            else
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                        }
                        else
                        {
                            if (electricViewModels.Count(e => e.LC <= 0) == 0)
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                            else
                            {
                                m = electricViewModels.Where(e => e.LC <= 0).First();
                            }
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        m.LC = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        m.LCisValid = valid == "1" ? true : false;
                        if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());

                        break;
                    #endregion
                    case "lc2":
                        #region
                        lc2Count++;
                        if (type == typeof(OrtDataModel))
                        {
                            if (electricViewModels.Count >= lc2Count)
                                m = electricViewModels[lc2Count - 1];
                            else
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                        }
                        else
                        {
                            if (electricViewModels.Count(e => e.LC2 <= 0) == 0)
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                            else
                            {
                                m = electricViewModels.Where(e => e.LC2 <= 0).First();
                            }
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        m.LC2 = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        m.LC2isValid = valid == "1" ? true : false;
                        if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "z":
                        #region
                        zCount++;
                        if (type == typeof(OrtDataModel))
                        {
                            if (electricViewModels.Count >= zCount)
                                m = electricViewModels[zCount - 1];
                            else
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                        }
                        else
                        {
                            if (electricViewModels.Count(e => e.Z <= 0) == 0)
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                            else
                            {
                                m = electricViewModels.Where(e => e.Z <= 0).First();
                            }
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        m.Z = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        m.ZisValid = valid == "1" ? true : false;
                        if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "z2":
                        #region
                        z2Count++;
                        if (type == typeof(OrtDataModel))
                        {
                            if (electricViewModels.Count >= z2Count)
                                m = electricViewModels[z2Count - 1];
                            else
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                        }
                        else
                        {
                            if (electricViewModels.Count(e => e.Z2 <= 0) == 0)
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                            else
                            {
                                m = electricViewModels.Where(e => e.Z2 <= 0).First();
                            }
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        m.Z2 = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        m.Z2isValid = valid == "1" ? true : false;
                        if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "esr":
                        #region
                        esrCount++;
                        if (type == typeof(OrtDataModel))
                        {
                            if (electricViewModels.Count >= esrCount)
                                m = electricViewModels[esrCount - 1];
                            else
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                        }
                        else
                        {
                            if (electricViewModels.Count(e => e.ESR <= 0) == 0)
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                            else
                            {
                                m = electricViewModels.Where(e => e.ESR <= 0).First();
                            }
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        m.ESR = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        m.ESRisValid = valid == "1" ? true : false;
                        if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "esr2":
                        #region
                        esr2Count++;
                        if (type == typeof(OrtDataModel))
                        {
                            if (electricViewModels.Count >= esr2Count)
                                m = electricViewModels[esr2Count - 1];
                            else
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                        }
                        else
                        {
                            if (electricViewModels.Count(e => e.ESR2 <= 0) == 0)
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                            else
                            {
                                m = electricViewModels.Where(e => e.ESR2 <= 0).First();
                            }
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        m.ESR2 = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        m.ESR2isValid = valid == "1" ? true : false;
                        if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "d"://D
                        #region
                        if (electricViewModels.Count(e => e.D <= 0) == 0)
                        {
                            m = new ElectricViewModel();
                            electricViewModels.Add(m);
                        }
                        else
                        {
                            m = electricViewModels.Where(e => e.D <= 0).First();
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        m.D = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        m.DisValid = valid == "1" ? true : false;
                        if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "l"://L
                        #region
                        if (electricViewModels.Count(e => e.L <= 0) == 0)
                        {
                            m = new ElectricViewModel();
                            electricViewModels.Add(m);
                        }
                        else
                        {
                            m = electricViewModels.Where(e => e.L <= 0).First();
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        m.L = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        m.LisValid = valid == "1" ? true : false;
                        if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "p"://P
                        #region
                        if (sizeViewModels.Count(e => e.P <= 0) == 0)
                        {
                            s = new SizeViewModel();
                            sizeViewModels.Add(s);
                        }
                        else
                        {
                            s = sizeViewModels.Where(e => e.P <= 0).First();
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        s.P = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        s.PisValid = valid == "1" ? true : false;
                        if (sNoProperty != null) s.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "cp"://P
                        #region
                        if (sizeViewModels.Count(e => e.cp <= 0) == 0)
                        {
                            s = new SizeViewModel();
                            sizeViewModels.Add(s);
                        }
                        else
                        {
                            s = sizeViewModels.Where(e => e.cp <= 0).First();
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        s.cp = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        s.cpisValid = valid == "1" ? true : false;
                        if (sNoProperty != null) s.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "h"://h
                        hCount++;
                        if (type == typeof(OrtDataModel))
                        {
                            #region ORT
                            if (electricViewModels.Count >= hCount)
                                m = electricViewModels[hCount - 1];
                            else
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }

                            //if (electricViewModels.Count(e => e.H <= 0) == 0)
                            //{
                            //    m = new ElectricViewModel();
                            //    electricViewModels.Add(m);
                            //}
                            //else
                            //{
                            //    m = electricViewModels.Where(e => e.H <= 0).First();
                            //}
                            value = 0d;
                            double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                            m.H = value;
                            valid = string.Empty;
                            valid = validProperty.GetValue(t, null).ToString();
                            m.HisValid = valid == "1" ? true : false;
                            if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                            #endregion
                        }
                        else //非ORT
                        {
                            #region
                            if (sizeViewModels.Count(e => e.H <= 0) == 0)
                            {
                                s = new SizeViewModel();
                                sizeViewModels.Add(s);
                            }
                            else
                            {
                                s = sizeViewModels.Where(e => e.H <= 0).First();
                            }
                            value = 0d;
                            double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                            s.H = value;
                            valid = string.Empty;
                            valid = validProperty.GetValue(t, null).ToString();
                            s.HisValid = valid == "1" ? true : false;
                            if (sNoProperty != null) s.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                            #endregion
                        }
                        break;
                    
                    case "w"://h
                        #region
                        if (sizeViewModels.Count(e => e.W <= 0) == 0)
                        {
                            s = new SizeViewModel();
                            sizeViewModels.Add(s);
                        }
                        else
                        {
                            s = sizeViewModels.Where(e => e.W <= 0).First();
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        s.W = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        s.WisValid = valid == "1" ? true : false;
                        if (sNoProperty != null) s.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "c"://h
                        #region
                        if (electricViewModels.Count(e => e.C <= 0) == 0)
                        {
                            m = new ElectricViewModel();
                            electricViewModels.Add(m);
                        }
                        else
                        {
                            m = electricViewModels.Where(e => e.C <= 0).First();
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        m.C = value;
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        m.CisValid = valid == "1" ? true : false;
                        if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "l1"://L1
                        l1Count++;
                        if (type == typeof(OrtDataModel))
                        {
                            #region ORT
                            if (electricViewModels.Count >= l1Count)
                                m = electricViewModels[l1Count - 1];
                            else
                            {
                                m = new ElectricViewModel();
                                electricViewModels.Add(m);
                            }
                            //if (electricViewModels.Count(e => string.IsNullOrWhiteSpace(e.L1)) == 0)
                            //{
                            //    m = new ElectricViewModel();
                            //    electricViewModels.Add(m);
                            //}
                            //else
                            //{
                            //    m = electricViewModels.Where(e => string.IsNullOrWhiteSpace(e.L1)).First();
                            //}
                            value = 0d;
                            double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                            valid = string.Empty;
                            valid = validProperty.GetValue(t, null).ToString();
                            if (value > 0)
                            {
                                m.L1 = value.ToString();
                                m.L1isValid = valid == "1" ? true : false;
                            }
                            else
                            {
                                m.L1 = valid;
                                m.L1isValid = true;
                            }
                            if (sNoProperty != null) m.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                            #endregion ORT
                        }
                        else //非ORT
                        {
                            #region
                            if (sizeViewModels.Count(e => string.IsNullOrWhiteSpace(e.L1)) == 0)
                            {
                                s = new SizeViewModel();
                                sizeViewModels.Add(s); 
                            }
                            else
                            {
                                s = sizeViewModels.Where(e => string.IsNullOrWhiteSpace(e.L1)).First();
                            }
                            value = 0d;
                            double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                            valid = string.Empty;
                            valid = validProperty.GetValue(t, null).ToString();
                            if (value > 0)
                            {
                                s.L1 = value.ToString();
                                s.L1isValid = valid == "1" ? true : false;
                            }
                            else
                            {
                                s.L1 = valid;
                                s.L1isValid = true;
                            }
                            if (sNoProperty != null) s.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                            #endregion
                        }

                        break;
                    case "l2"://L2
                        #region
                        if (sizeViewModels.Count(e => string.IsNullOrWhiteSpace(e.L2)) == 0)
                        {
                            s = new SizeViewModel();
                            sizeViewModels.Add(s);
                        }
                        else
                        {
                            s = sizeViewModels.Where(e => string.IsNullOrWhiteSpace(e.L2)).First();
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        if (value > 0)
                        {
                            s.L2 = value.ToString();
                            s.L2isValid = valid == "1" ? true : false;
                        }
                        else
                        {
                            s.L2 = valid;
                            s.L2isValid = true;
                        }
                        if (sNoProperty != null) s.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    case "l3"://L3
                        #region
                        if (sizeViewModels.Count(e => string.IsNullOrWhiteSpace(e.L3)) == 0)
                        {
                            s = new SizeViewModel();
                            sizeViewModels.Add(s);
                        }
                        else
                        {
                            s = sizeViewModels.Where(e => string.IsNullOrWhiteSpace(e.L3)).First();
                        }
                        value = 0d;
                        double.TryParse(valueProperty.GetValue(t, null).ToString(), out value);
                        valid = string.Empty;
                        valid = validProperty.GetValue(t, null).ToString();
                        if (value > 0)
                        {
                            s.L3 = value.ToString();
                            s.L3isValid = valid == "1" ? true : false;
                        }
                        else
                        {
                            s.L3 = valid;
                            s.L3isValid = true;
                        }
                        if (sNoProperty != null) s.SNo = int.Parse(sNoProperty.GetValue(t, null).ToString());
                        break;
                    #endregion
                    default:
                        break;
                }

                if (headIdProperty != null && m != null) m.HeadId = int.Parse(headIdProperty.GetValue(t, null).ToString());
            }
        }

        public static void GenerateTestList(int electricQty, int sizeQty, int sNo
            , List<ElectricViewModel> electricViewModels
            , List<SizeViewModel> sizeViewModels)
        {
            if (electricViewModels == null) electricViewModels = new List<ElectricViewModel>();
            int count = electricQty - electricViewModels.Count;
            if (electricQty > 0 && count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    ElectricViewModel ev = new ElectricViewModel();
                    ev.SNo = sNo;
                    electricViewModels.Add(ev);
                }
            }

            if (sizeViewModels == null) sizeViewModels = new List<SizeViewModel>();
            count = sizeQty - sizeViewModels.Count;
            if (sizeQty > 0 && count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    SizeViewModel s = new SizeViewModel();
                    s.SNo = sNo;
                    sizeViewModels.Add(s);
                }
            }
        }
    }



    public enum ItemCode
    {
        CAP,
        DF,
        LC,
        Z,
        ESR,
        D,
        L,
        P,
        cp,
        H,
        W,
        C,
        L1,
        L2,
        L3,
        Z2,
        LC2,
        ESR2
    }
}
